(function(angular) {
	'use strict';
	/**
	 * this service for filter 
	 * for ap setup such as product/apProgram/apTradepartner/apCommunity/apMember
	 */
	angular.module('fs.waf').factory('profFilterOptionService', profFilterOptionService)
	.filter("intBaseTypeFilter", [ '$filter', '$locale', function($filter, $locale) {
		return function(optionValue, pattern, options) {
			//if intRateType == P or A FIXED PERCENTAGE is P, FIXED AMOUNT is A
			if('P' == pattern || 'A' == pattern) {
				return '';
			} else {
				var optionLabel = '';
				if(options && angular.isArray(options)) {
					for(var i = 0; i < options.length; i++){
						if($filter('encode')(optionValue) == $filter('encode')(options[i].value)){
							optionLabel = $filter('translate')(options[i].label);
							break;
						}
					}
				}
				return optionLabel;
			}
		};
	} ])
	.filter("intRateIndexFilter", [ '$filter', '$locale', function($filter, $locale) {
		return function(optionValue, pattern) {
			//if intRateType == P or A FIXED PERCENTAGE is P, FIXED AMOUNT is A
			if('P' == pattern || 'A' == pattern) {
				return '';
			} else {
				return optionValue;
			}
		};
	} ])
	.filter("intSpreadTypeFilter", [ '$filter', '$locale', function($filter, $locale) {
		return function(optionValue, pattern, options) {
			//if intRateType == P or A FIXED PERCENTAGE is P, FIXED AMOUNT is A
			if('P' == pattern || 'A' == pattern) {
				return '';
			} else {
				var optionLabel = '';
				if(options && angular.isArray(options)) {
					for(var i = 0; i < options.length; i++){
						if($filter('encode')(optionValue) == $filter('encode')(options[i].value)){
							optionLabel = $filter('translate')(options[i].label);
							break;
						}
					}
				}
				return optionLabel;
			}
		};
	} ])
	;
	profFilterOptionService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'modalService', 'gridService', 'arithService' ];
	function profFilterOptionService($q, loggerFactory, commonService, ajaxServiceFactory, modalService, gridService, arithService) {
		var log = loggerFactory.getLogger("profFilterOptionService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfFilterOptions : {
				url : 'do/cda/dataList/profFilterOptions',
				method : 'get'
			},
			getCommunityListFilterOptions : {
				url : 'do/cda/dataList/communityListFilterOptions',
				method : 'get'
			},
		});
		function getProfFilterOptions() {
			var defer = $q.defer();
			ajaxService.getProfFilterOptions().then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		function getCommunityListFilterOptions() {
			var defer = $q.defer();
			ajaxService.getCommunityListFilterOptions().then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		
		function initProfFilterOptions(ctrl) {
			return $q(function(resolved, rejected) {
				getProfFilterOptions().then(function(response) {
					// interest
					ctrl.intCollMethodOptions = response.intCollMethodOptions;
					ctrl.intRateTypeOptions = response.intRateTypeOptions;
					ctrl.intBaseTypeOptions = response.intBaseTypeOptions;
					ctrl.intSpreadTypeOptions = response.intSpreadTypeOptions;
					// pastdue
					ctrl.intOnGraceperdOptions = response.intOnGraceperdOptions;
					ctrl.pastdueIntCatOptions = response.pastdueIntCatOptions;
					ctrl.intBookTypeOptions = response.intBookTypeOptions;
					ctrl.allocCodeOptions = response.allocCodeOptions;
					// doc
					ctrl.templateNameOptions = response.templateNameOptions;
					ctrl.docTypeOptions = response.docTypeOptions;
					ctrl.deliveryTypeOptions = response.deliveryTypeOptions;
					// facility
					ctrl.facilityTypeOptions = response.facilityTypeOptions;
					// profSetlinst
					ctrl.setlinstItemTypeOptions = response.setlinstItemTypeOptions;
					ctrl.setlinstCrDrOptions = response.setlinstCrDrOptions;
					// memberStatus
					ctrl.memberStatusOptions = response.memberStatusOptions;
					
					ctrl.localeCodeOptions = response.localeCodeOptions;
					resolved();
				}, angular.noop);
			});
		};
		
		function initCommunityListFilterOptions(ctrl) {
			return $q(function(resolved, rejected) {
				getCommunityListFilterOptions().then(function(response) {
					ctrl.communityStatusOptions = response.communityStatusOptions;
					ctrl.communityCodeQOptions = response.communityCodeQOptions;
					resolved();
				});
			});
		};
		
		
		return {
			initProfFilterOptions : initProfFilterOptions,
			initCommunityListFilterOptions : initCommunityListFilterOptions,
		};
	}
})(angular);